<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<% response.setContentType("text/html; charset=UTF-8");%>
<% request.setCharacterEncoding("utf-8");%>
<%@ page import="java.sql.*" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.*" %>
<%@ page import="java.io.File" %>
<%
	Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(getServletContext().getRealPath("/") + "web.xml"));
	doc.getDocumentElement().normalize();
	String dbhost = doc.getElementsByTagName("host").item(0).getTextContent();
	String dbport = doc.getElementsByTagName("port").item(0).getTextContent();
	String dbuser = doc.getElementsByTagName("user").item(0).getTextContent();
	String dbpass = doc.getElementsByTagName("pass").item(0).getTextContent();
	String dbname = doc.getElementsByTagName("database").item(0).getTextContent();
	String dbconnString = "jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname + "?setUnicode=true&characterEncoding=UTF-8";
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	Connection dbconn = DriverManager.getConnection(dbconnString, dbuser, dbpass);
	Statement query = dbconn.createStatement();
%>

<%
	String tenloai = request.getParameter("tenloai");
	String thoigian = request.getParameter("thoigian");
	String socaude = request.getParameter("socaude");
	String socautb = request.getParameter("socautb");
	String socaukho = request.getParameter("socaukho");
	if (tenloai != null && thoigian != null && socaude != null && socautb != null && socaukho != null) {
		try {
			ResultSet rs = query.executeQuery("select max(maLDT) as max from loaidethi");
			int max = 0;
			rs.next();
			max = rs.getInt("max");
			rs.close();
			String maLDT = Integer.toString(max + 1);
			query.executeUpdate("insert into loaidethi (maLDT, tenloai, thoigian, socaude, socautb, socaukho) values ( " 
					+ maLDT + " , '" + tenloai + "' , " + thoigian + " , " + socaude + " , " + socautb + " , " + socaukho + " )");
			out.println("Phát sinh thành công. Mã loại đề thi : " + maLDT); // phát sinh thành công
			response.sendRedirect("thi_dethi_phatsinh.jsp?maldt=" + maLDT);
		} catch (Exception e) {
			out.println(e.getMessage());
		}
	}
%>

<% dbconn.close();%>